class Solution:
    def reversePairs(self, nums: list) -> int:
        def binaryImport(nums, i, j, tar):
            if j - i <= 1:
                if tar > nums[j] * 2:
                    return j
                if tar > nums[i] * 2:
                    return i
                return -1
            mid = (i + j) // 2
            if nums[mid] * 2 >= tar:
                return binaryImport(nums, i, mid - 1, tar)
            else:
                return binaryImport(nums, mid, j, tar)

        def binaryIndex(nums, i, j, tar):
            if j - i <= 1:
                if tar >= nums[j]:
                    return j + 1
                if tar >= nums[i]:
                    return i + 1
                return i
            mid = (i + j) // 2
            if nums[mid] > tar:
                return binaryIndex(nums, i, mid - 1, tar)
            else:
                return binaryIndex(nums, mid, j, tar)

        if len(nums) == 0:
            return 0
        array = []
        array.append(nums[-1])
        res = 0
        for i in range(len(nums) - 2, -1, -1):
            arrayImport = binaryImport(array, 0, len(array) - 1, nums[i])
            res += (arrayImport + 1)
            arrayIndex = binaryIndex(array, 0, len(array) - 1, nums[i])
            array.insert(arrayIndex, nums[i])
        return res
